// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html
//{"id":"10","level":"null","unLock":"0","getMethod":"gold","videoCount":"null","gold":"20000","name":"11"},
//{"id":"11","level":"null","unLock":"0","getMethod":"gold","videoCount":"null","gold":"20000","name":"12"},
//{"id":"12","level":"null","unLock":"0","getMethod":"gold","videoCount":"null","gold":"20000","name":"13"},
//{"id":"13","level":"null","unLock":"0","getMethod":"gold","videoCount":"null","gold":"20000","name":"14"},
//{"id":"14","level":"null","unLock":"0","getMethod":"video","videoCount":[0,5],"gold":"0","name":"15"},
//{"id":"15","level":"null","unLock":"0","getMethod":"video","videoCount":[0,5],"gold":"0","name":"16"},
//{"id":"16","level":"null","unLock":"0","getMethod":"video","videoCount":[0,5],"gold":"0","name":"17"},
//{"id":"17","level":"null","unLock":"0","getMethod":"video","videoCount":[0,5],"gold":"0","name":"18"},
//{"id":"18","level":"3","unLock":"0","getMethod":"video","videoCount":[0,1],"gold":"0","name":"19"},
//{"id":"19","level":"15","unLock":"0","getMethod":"video","videoCount":[0,1],"gold":"0","name":"20"},
//{"id":"20","level":"25","unLock":"0","getMethod":"video","videoCount":[0,1],"gold":"0","name":"21"},
//{"id":"21","level":"35","unLock":"0","getMethod":"video","videoCount":[0,1],"gold":"0","name":"22"},
//{"id":"22","level":"45","unLock":"0","getMethod":"video","videoCount":[0,3],"gold":"0","name":"23"},
//{"id":"23","level":"55","unLock":"0","getMethod":"video","videoCount":[0,3],"gold":"0","name":"24"},
//{"id":"24","level":"65","unLock":"0","getMethod":"video","videoCount":[0,3],"gold":"0","name":"25"},
//{"id":"25","level":"75","unLock":"0","getMethod":"video","videoCount":[0,5],"gold":"0","name":"26"},
//{"id":"26","level":"85","unLock":"0","getMethod":"video","videoCount":[0,5],"gold":"0","name":"27"}
import GameData_red_blue_man, {dataName, emitType} from "./GameData_red_blue_man";
import SkinNode from "./SkinNode";
import Label = cc.Label;
import Money from "./Money";
import ControlScript from "../PublicScript/ControlScript";
import {animName} from "./HeroControl";
import Music, {musicName} from "../PublicScript/Music";

const {ccclass, property} = cc._decorator;

@ccclass
export default class SkinShop extends cc.Component {

    @property({tooltip: "金币皮肤", type: cc.Node})
    goldSkin: cc.Node = null;
    @property({tooltip: "关卡皮肤", type: cc.Node})
    levelSkin: cc.Node = null;
    @property({tooltip: "皮肤类型列表", type: cc.Node})
    skinTypePage: cc.Node[] = [];
    @property({tooltip: "皮肤节点模板", type: cc.Node})
    skinNodeTemplate: cc.Node = null;
    @property({tooltip: "皮肤展示节点", type: sp.Skeleton})
    skinShow: sp.Skeleton[] = [];
    @property({tooltip: "可替换的图片", type: cc.SpriteFrame})
    btnSpriteFrame: cc.SpriteFrame[] = [];
    @property({tooltip: "所有的按钮", type: cc.Node})
    btnList: cc.Node[] = [];
    @property({tooltip: "金币数量", type: cc.Label})
    goldLabel: cc.Label = null;
    @property({tooltip: "视频次数", type: cc.Label})
    videoLabel: cc.Label = null;
    @property({tooltip: "提示语句", type: cc.Node})
    tipsLabel: cc.Node = null;
    //选中的按钮
    selectBtn = 0;
    //解锁皮肤需要花费的金币数量
    unLockSkinGoldCount = 0;
    //解锁皮肤需要花费的视频次数
    unLockSkinVideoCount = [];
    //被使用的皮肤
    usingSkinNode = null;
    //对应在使用的皮肤的编号
    usingSkinIndex = 0;
    //试用皮肤
    static experienceSkinIndex:any = null;

    onLoad () {
        this.init();
        cc.game.on(emitType.changeShowSkin, this.changeShowMan, this);
    }

    start () {

    }

    init(){
        this.levelSkin.scale = 0.8;
        this.skinTypePage[1].active = false;
        this.createSkinBtn();
        this.closeAllBtn();
        this.btnList[1].active = true;
        this.usingSkinIndex = GameData_red_blue_man.getUsingSkinIndex();
    }

    onClick(e,d){
        Music.GamePlayEffect(musicName.click);
        if(parseInt(d) == 1){       //金币皮肤界面
            this.btnChange(0);
            this.tipsLabel.active = false;
        }
        if(parseInt(d) == 2){       //关卡皮肤界面
            this.btnChange(1);
            this.tipsLabel.active = true;
        }
        if(parseInt(d) == 3){       //金币解锁
            this.goldUnLock();
        }
        if(parseInt(d) == 4){       //使用
            this.btnList[2].active = false;
            this.btnList[1].active = true;

            if(this.usingSkinNode != this.lastNode && this.usingSkinNode != null){
                this.usingSkinNode.getComponent(SkinNode).changeBgSprite(this.btnSpriteFrame[0]);
            }
            this.usingSkinNode = this.lastNode;
            this.lastNode.getComponent(SkinNode).changeBgSprite(this.btnSpriteFrame[2]);
            this.usingSkinIndex = this.usingSkinNode.getComponent(SkinNode).skinNum;
            GameData_red_blue_man.usingSkinIndex = this.usingSkinIndex;
            //存入正在使用的皮肤
            GameData_red_blue_man.saveUsingSkinIndex();
            this.showAnim(this.skinShow[0]);
            this.showAnim(this.skinShow[1]);
            cc.game.emit(emitType.changeMenuSkin);
        }
        if(parseInt(d) == 5){       //视频解锁
            this.videoUnLock();
        }
        if(parseInt(d) == 6){       //体验皮肤
            GameData_red_blue_man.showVideo({callback: ()=>{
                    SkinShop.experienceSkinIndex = GameData_red_blue_man.skinData[this.dataIndex].name;
                    cc.director.loadScene("GameScene");
                }, name: "商店视频体验皮肤"});
        }
    }

    showAnim(node){
        let anim = node.setAnimation(1,animName.celebrate2,false);
        node.setTrackCompleteListener(anim, ()=>{
            node.setAnimation(1,animName.idle,true);
        });
    }

    btnChange(num){
        if(this.selectBtn == num){
            return;
        }
        this.selectBtn = num;
        this.goldSkin.scale = this.selectBtn == 0 ? 1 : 0.8;
        this.levelSkin.scale = this.selectBtn == 1 ? 1 : 0.8;
        this.skinTypePage[0].active = this.selectBtn == 0;
        this.skinTypePage[1].active = this.selectBtn == 1;
    }

    //生成皮肤按钮
    createSkinBtn(){
        let name1 = "blue" + GameData_red_blue_man.usingSkinIndex;
        let name2 = "red" + GameData_red_blue_man.usingSkinIndex;
        this.skinShow[0].setSkin(name1);
        this.skinShow[1].setSkin(name2);
        for(let i = 0; i < GameData_red_blue_man.skinSum; i++){
            let view = cc.instantiate(this.skinNodeTemplate);
            let lock = false;
            if(GameData_red_blue_man.skinData[i].unLock == 0 || GameData_red_blue_man.skinData[i].unLock == 2){
                lock = true;
            }
            if(GameData_red_blue_man.usingSkinIndex == GameData_red_blue_man.skinData[i].name){
                view.getComponent(SkinNode).changeBgSprite(this.btnSpriteFrame[2]);
                this.usingSkinNode = view;
            }
            view.getComponent(SkinNode).setSkinNum(GameData_red_blue_man.skinData[i].name, lock, i);
            view.active = true;
            if(GameData_red_blue_man.skinData[i].getMethod == "gold"){
                view.getChildByName("lock").getChildByName("level").active = false;
                view.parent = this.skinTypePage[0].children[0].children[0];
            }else{
                view.parent = this.skinTypePage[1].children[0].children[0];
                if(GameData_red_blue_man.skinData[i].level != "null"){
                    view.getChildByName("lock").getChildByName("level").getChildByName("label").getComponent(cc.Label).string = GameData_red_blue_man.skinData[i].level;
                }else{
                    view.getChildByName("lock").getChildByName("level").active = false;
                }
            }
        }
    }

    lastNode = null;
    //对应的数据编号
    dataIndex = 0;
    changeShowMan(num, node, dataNum){
        this.dataIndex = dataNum;
        if(this.lastNode){
            if(this.usingSkinNode != this.lastNode){
                this.lastNode.getComponent(SkinNode).changeBgSprite(this.btnSpriteFrame[0]);
            }
        }
        if(num == 0){
            num = 1
        }
        let name1 = "blue"+num;
        let name2 = "red"+num;
        this.lastNode = node;
        if(this.usingSkinNode != this.lastNode){
            node.getComponent(SkinNode).changeBgSprite(this.btnSpriteFrame[1]);
        }
        this.skinShow[0].setSkin(name1);
        this.skinShow[1].setSkin(name2);
        this.skinShow[0].setAnimation(1, animName.idle, true);
        this.skinShow[1].setAnimation(1, animName.idle, true);
        this.closeAllBtn();
        if(this.usingSkinNode == this.lastNode){
            this.btnList[1].active = true;
            return;
        }
        if(GameData_red_blue_man.skinData[dataNum].unLock){
            if(dataNum == 4 || dataNum == 6){
                this.closeAllBtn();
                if(GameData_red_blue_man.skinData[dataNum].unLock == 1){
                    this.btnList[2].active = true;
                }else{
                    this.btnList[5].active = true;
                }
                return;
            }
            //判断解锁条件
            if(GameData_red_blue_man.skinData[dataNum].getMethod == "gold"){         //金币
                if(GameData_red_blue_man.skinData[dataNum].unLock == 1){
                    this.closeAllBtn();
                    this.btnList[2].active = true;
                }else{
                    this.btnList[0].active = true;
                    this.btnList[4].active = true;
                    this.goldLabel.string = GameData_red_blue_man.skinData[dataNum].gold;
                }
            }else if(GameData_red_blue_man.skinData[dataNum].getMethod == "video"){      //视频
                if(GameData_red_blue_man.skinData[dataNum].unLock == 1){
                    this.closeAllBtn();
                    this.btnList[2].active = true;
                }else{
                    if(GameData_red_blue_man.skinData[dataNum].unLock == 0 && GameData_red_blue_man.skinData[dataNum].level != "null"){
                        this.btnList[3].children[0].getComponent(cc.Button).interactable = false;
                        this.btnList[3].getChildByName("finger").active = false;
                    }else{
                        let rand = Math.random() < 0.5 ? 3 : 4;
                        this.btnList[rand].getChildByName("finger").active = false;
                    }
                    this.btnList[3].active = true;
                    this.btnList[4].active = true;
                    this.showLabel();
                }
            }
        }else{
            this.btnList[2].active = true;
        }
    }

    closeAllBtn(){
        this.btnList[3].getChildByName("finger").active = true;
        this.btnList[4].getChildByName("finger").active = true;
        this.btnList[3].children[0].getComponent(cc.Button).interactable = true;
        for(let i of this.btnList){
            i.active = false;
        }
    }

    showLabel(){
        this.videoLabel.string = GameData_red_blue_man.skinData[this.dataIndex].videoCount[0] + ";" + GameData_red_blue_man.skinData[this.dataIndex].videoCount[1];
    }

    //金币解锁
    goldUnLock(){
        let num = parseInt(GameData_red_blue_man.skinData[this.dataIndex].gold);
        Money.usingMoney(num, ()=>{
            GameData_red_blue_man.skinData[this.dataIndex].unLock = 1;
            GameData_red_blue_man.lockSkinNum.splice(GameData_red_blue_man.lockSkinNum.indexOf(GameData_red_blue_man.skinData[this.dataIndex].name), 1);
            this.closeAllBtn();
            this.btnList[2].active = true;
            GameData_red_blue_man.saveSkinData();
            this.lastNode.getChildByName("lock").active = false;
        });
    }

    //视频解锁
    videoUnLock(){
        GameData_red_blue_man.showVideo({callback:()=>{
                GameData_red_blue_man.skinData[this.dataIndex].videoCount[0] += 1;
                this.showLabel();
                if(GameData_red_blue_man.skinData[this.dataIndex].videoCount[0] >= GameData_red_blue_man.skinData[this.dataIndex].videoCount[1]){
                    this.closeAllBtn();
                    GameData_red_blue_man.lockSkinNum.splice(GameData_red_blue_man.lockSkinNum.indexOf(GameData_red_blue_man.skinData[this.dataIndex].name), 1);
                    console.log(GameData_red_blue_man.lockSkinNum);
                    this.btnList[2].active = true;
                    GameData_red_blue_man.skinData[this.dataIndex].unLock = 1;
                    this.lastNode.getChildByName("lock").active = false;
                    Money.addMoney(200);
                    ControlScript.createToast("获得200金币，成功解锁皮肤！")
                }else{
                    Money.addMoney(200);
                    ControlScript.createToast("获得200金币，皮肤累计解锁次数+1！")
                }
                GameData_red_blue_man.saveSkinData();
            }, name: "视频解锁皮肤"})
    }

    callback = null;
    setCallback(call){
        this.callback = call;
        console.log(this.callback);
    }

    onClickCall(){
        if(this.callback){
            this.callback();
        }else{
            this.node.destroy();
        }
    }

    // update (dt) {}
}
